package com.ddd.mill.infrastructure.persistent.repository.impl.sku;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ddd.mill.domain.aggregate.sku.entity.ProductSku;
import com.ddd.mill.domain.aggregate.sku.entity.ProductSkuValue;
import com.ddd.mill.domain.aggregate.sku.repository.ProductSkuValueRepository;
import com.ddd.mill.infrastructure.persistent.dao.product.ProductSkuValueDao;
import com.ddd.mill.infrastructure.utils.TypeConvertUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author liu
 * @title: ProductSkuValueMyBatisRepositoryImpl
 * @projectName trading-center
 * @date 2020/11/3010:25
 */
public class ProductSkuValueMyBatisRepositoryImpl implements ProductSkuValueRepository {

    @Resource
    ProductSkuValueDao dao;
    @Override
    public ProductSkuValue load(Long id) {
        return dao.selectById(id);
    }

    @Override
    public Long save(ProductSkuValue entity) {
        if (!Objects.isNull(entity.getId()) && !Objects.isNull(dao.selectById(entity.getId()))) {
            dao.updateById(entity);
        } else {
            dao.insert(entity);
        }
        return entity.getId();
    }


    @Override
    public void saveOrUpdate(Long productSkuId, String[] attributeIds) {

    }

    @Override
    public Map<Long, List<ProductSkuValue>> getListBySkuList(List<ProductSku> productSkuList) {
        QueryWrapper<ProductSkuValue> queryWrapper = new QueryWrapper();
        queryWrapper.in("sku_id", TypeConvertUtils.convertToSet(productSkuList, "id"));
        return TypeConvertUtils.convertVL(dao.selectList(queryWrapper), "skuId");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<ProductSkuValue> productSkuValueList) {
        for (ProductSkuValue productSkuValue:productSkuValueList){
            this.save(productSkuValue);
        }
    }
}
